﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//
// luck抽奖软件
// Windows平台下满足小规模企业或组织基本需求的抽奖软件
//
// 软件版本: 0.1.0
// 作者: cikker
//

namespace luck
{
    /// <summary>
    /// 抽奖软件界面
    /// 
    /// 创建: 2018-02-15 12:59 除夕
    /// 更新: 2018-03-03 16:59
    /// 作者: cikker
    /// 类版本: 0.0.2
    /// </summary>
    public partial class UI : Form
    {
        luck my_luck;
        int sta = 0;

        public UI()
        {
            InitializeComponent();

            my_luck = new luck();
            my_luck.Alarm += new luck.AlarmEventHandler(display);
        }

        /// <summary>
        /// 由抽奖模块触发的显示事件
        /// 根据实际需求显示相关数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void display(object sender, EventArgs e)
        {
            SetText(my_luck.name_now);
            SetAllText(my_luck.luck_now_all_name);
        }

        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (label1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                label1.Text = text;
            }
        }

        private void SetAllText(string text)
        {
            if (label1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetAllText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                label3.Text = my_luck.luck_now_all_name;
            }
        }
        
        private void UI_KeyDown(object sender, KeyEventArgs e)
        {
            // Alt + P控制开始和停止抽奖 组合键的目的是防止误触误操作
            if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.P)
            {
                // 开始抽奖
                if (sta == 0)
                {
                    sta = 1;
                    my_luck.start();

                    if (my_luck.luck_now != "end")
                    {
                        label2.Text = my_luck.luck_now + " 抽奖中...";                        
                    }
                    else
                    {
                        label2.Text = "抽奖结束";
                    }
                    label3.Visible = false;
                }
                // 暂停抽奖且当前已抽出一个中奖人员
                else if (sta == 1)
                {
                    sta = 0;
                    my_luck.stop();

                    if (my_luck.luck_now != "end")
                    {
                        label2.Text = my_luck.luck_now;
                    }
                    else
                    {
                        label2.Text = "抽奖结束";
                    }
                    label3.Visible = true;                 
                }
            }
        }

        private void UI_FormClosing(object sender, FormClosingEventArgs e)
        {
            my_luck.exit();
        }
    }
}
